import { Config } from "@/config/Config";
import { HttpClient } from "@/httpclient/HttpClient";
import { DateUtil } from "@/utils/DateUtil";
import { Logger } from "@/utils/Logger";
import { RandomUtil } from "@/utils/RandomUtil";

export class OpenServerData {


    public startTime: number; //开始时间
    public endTime: number; //结束时间
    public registerNum: number; //注册人数
    public createNum: number; //创角数
    public loginNum: number; //登录人数
    public chargeMoney: number; //付费金额
    public newChargePeople: number; //新增付费人数
    public arpu: string; //付费arpu    统计每个小时内付费金额/付费人数
    public newChargePercent: string; //新增付费率

    public static open_sever_time: number;  //开服时间戳
    public static no_duplicate_login_num: number; //去重登录数
    public static no_duplicate_new_pay_role_num: number; //去重新增支付人数


    public static modelList: Array<OpenServerData> = [];

    public static totalNum: number = 10;

    public static initTest() {
        this.modelList = [];
        this.open_sever_time = DateUtil.now();
        this.no_duplicate_login_num = 0;
        this.no_duplicate_new_pay_role_num = 0;
        for (let i = 0; i < this.totalNum; i++) {
            let model: OpenServerData = new OpenServerData();
            model.startTime = DateUtil.now() + i * 60 * 60 * 1000;
            model.endTime = model.startTime + 60 * 60 * 1000
            model.registerNum = RandomUtil.nextInt(1, 5555);
            model.createNum = RandomUtil.nextInt(1, 5555);
            model.loginNum = RandomUtil.nextInt(1, 5555);
            model.chargeMoney = RandomUtil.nextInt(1, 555555)
            model.newChargePeople = RandomUtil.nextInt(1, 555454);
            model.arpu = (model.chargeMoney / model.newChargePeople ).toFixed(2) + ""
            model.newChargePercent = (model.chargeMoney / model.newChargePeople*100 ).toFixed(2) + "%"
            this.modelList[i] = model;
        }
    }

    public static json2ModelList(jsonData: object) {
        let data: Array<object> = jsonData["daily_data"]
        this.open_sever_time = jsonData["open_sever_time"] * 1000
        this.no_duplicate_login_num = jsonData["no_duplicate_login_num"]
        this.no_duplicate_new_pay_role_num = jsonData["no_duplicate_new_pay_role_num"]
        this.modelList = [];
        for (let i = 0; i < data.length; i++) {
            let model: OpenServerData = new OpenServerData();
            model.startTime = data[i]["start_time"] * 1000
            model.endTime = model.startTime + 60 * 60 * 1000
            model.registerNum = data[i]["register_num"]
            model.createNum = data[i]["create_num"]
            model.loginNum = data[i]["login_num"]
            model.chargeMoney = data[i]["pay_num"]
            model.newChargePeople = data[i]["new_pay_role_num"]
            model.arpu = (model.chargeMoney / model.newChargePeople ).toFixed(2) + ""
            model.newChargePercent = (model.chargeMoney / model.newChargePeople*100 ).toFixed(2) + "%"
            if (model.newChargePeople == 0) {
                model.arpu = "0"
                model.newChargePercent = "0%"
            }
            this.modelList[i] = model;
        }
    }


    public static getShowList(selectServer: number, listQuery: any, callback: Function) {
        let showList: Array<OpenServerData> = [];
        if (Config.isConnectServer) {
            Logger.log("OpenServerData.getShowList", selectServer)
            OpenServerData.totalNum = 0;
            OpenServerData.open_sever_time = 0;
            if(selectServer > 0){
                HttpClient.query_open_server_data_view(selectServer, ()=>{
                    showList = OpenServerData.modelList;
                    callback && callback(showList)
                });
            }else{
                callback && callback(showList)
            }
        }
        else {
            let startIndex: number = (listQuery["page"] - 1) * listQuery["limit"]
            let endIndex: number = startIndex + listQuery["limit"]
            for (let i = startIndex; i < endIndex; i++) {
                let model: OpenServerData = this.modelList[i];
                if (model) {
                    showList.push(model)
                }
            }
            callback && callback(showList)
        }
    }

    public static getTotalNewDevice() {
        let totalNum: number = 0;
        for (let i = 0; i < this.modelList.length; i++) {
            let model: OpenServerData = this.modelList[i];
        }
        return totalNum;
    }

    public static getTotalregisterNum() {
        let totalNum: number = 0;
        for (let i = 0; i < this.modelList.length; i++) {
            let model: OpenServerData = this.modelList[i];
            totalNum += model.registerNum;
        }
        return totalNum;
    }

    public static getTotalcreateNum() {
        let totalNum: number = 0;
        for (let i = 0; i < this.modelList.length; i++) {
            let model: OpenServerData = this.modelList[i];
            totalNum += model.createNum;
        }
        return totalNum;
    }

    public static getTotalloginNum() {
        let totalNum: number = 0;
        // for (let i = 0; i < this.modelList.length; i++) {
        //     let model: OpenServerData = this.modelList[i];
        //     totalNum += model.loginNum;
        // }
        // return totalNum;
        return this.no_duplicate_login_num
    }

    public static getTotalchargeMoney() {
        let totalNum: number = 0;
        for (let i = 0; i < this.modelList.length; i++) {
            let model: OpenServerData = this.modelList[i];
            totalNum += model.chargeMoney;
        }
        return totalNum;
    }

    public static getTotalnewChargePeople() {
        let totalNum: number = 0;
        // for (let i = 0; i < this.modelList.length; i++) {
        //     let model: OpenServerData = this.modelList[i];
        //     totalNum += model.newChargePeople;
        // }
        // return totalNum;
        return this.no_duplicate_new_pay_role_num
    }

    public static getTotalarpu() {
        let totalChargePeople: number = this.getTotalnewChargePeople();
        let str: string = (this.getTotalchargeMoney() / this.getTotalnewChargePeople() ).toFixed(2) + ""
        if (totalChargePeople == 0) {
            str = "0"
        }
        return str;
    }

    public static getNewChargePercent() {
        let totalRegNum: number = this.getTotalregisterNum()
        let str: string = (this.getTotalnewChargePeople() / totalRegNum * 100).toFixed(2) + "%"
        if (totalRegNum == 0) {
            str = "0%"
        }
        return str;
    }
}